package com.bnlr.lrparking.appui.order.stages;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.event.IsStallPayEvent;
import com.bnlr.lrparking.event.StallPaySucceeEvent;
import com.bnlr.lrparking.model.BaseArrayBean;
import com.bnlr.lrparking.model.WantBuyBean;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.appui.carport.purpose.MyPurposeActivity;
import com.bnlr.lrparking.view.ChineseEditText;
import com.bnlr.lrparking.widget.boarddialog.KeyBoardDialog;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author hp
 * @date 2017/10/17
 * 我要买 提交信息
 */

public class SubmitMessageActivity extends BaseActivity {

    private Unbinder unbinder;
    @BindView(R.id.et_name)
    ChineseEditText etName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    private String spaceId;
    private String username ,publishPhone,realName;
    private int fromTag;
    private WantBuyBean bean;
    private int tag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submitmessage);
        EventBus.getDefault().register(this);
        unbinder = ButterKnife.bind(this);
        username = AppConfig.getInstance().getString("userName","");
        realName = AppConfig.getInstance().getString("realName","");
        publishPhone = username;
        etName.setText(realName);
        if (!username.equals("")) {
            tvPhone.setText(username.substring(0, 3) + "****" + username.substring(7));
        }
        initView();
    }
    @OnClick({R.id.id_submit,R.id.tv_phone,R.id.submit_rl})
    public void submitMessageClick (View view) {
        switch (view.getId()) {
            case R.id.id_submit:
                finish();
                break;
            case R.id.tv_phone:
                KeyBoardDialog dialog = new KeyBoardDialog(this,publishPhone);
                dialog.setOnDialogResultListener(new KeyBoardDialog.OnDialogResultListener() {
                    @Override
                    public void onResult(String result) {
                        if (result.equals("some")) {
                            ToastUtils.showShort("此手机号无需再次验证");
                        } else if (!result.equals("fail")) {
                            publishPhone = result;
                            tvPhone.setText(publishPhone.substring(0, 3) + "****" + publishPhone.substring(7));
                        }
                    }
                });
                dialog.show();
                break;
            case R.id.submit_rl:
                if (TextUtils.isEmpty(etName.getText().toString())) {
                    ToastUtils.showShort("请填写联系人");
                    return;
                }
                getSubmit();
                break;
            default:
                break;
        }
    }
    private void initView() {
        Intent intent = getIntent();
        spaceId = intent.getStringExtra("spaceId");
        fromTag = intent.getIntExtra("buy_from",0);
        bean = new WantBuyBean();
        if (fromTag == 1) {
            bean.setIsByStage(0);
            tag = intent.getIntExtra("typeInt",0);
        } else if (fromTag == 2) {
            bean.setIsByStage(1);
            bean.setBankId(intent.getIntExtra("bankId",0));
            bean.setFirstPay(intent.getStringExtra("firstPay"));
            bean.setEachStagePrice(intent.getStringExtra("eachStagePrice"));
            bean.setStageNumber(intent.getIntExtra("stageNumber",0));
        }
        bean.setSpaceId(spaceId);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCLose(StallPaySucceeEvent event) {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onIsPay(IsStallPayEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);

    }

    private void getSubmit() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "AddUserWantSpace");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("spaceId", spaceId);
            mRequest.add("name", etName.getText().toString() + (radioGroup.getCheckedRadioButtonId()==R.id.man?"先生":"女士"));
            mRequest.add("phone", publishPhone);
            // 添加到请求队列
            CallServer.getRequestInstance().add(SubmitMessageActivity.this, mRequest,
                    new CustomHttpListener<BaseArrayBean>(SubmitMessageActivity.this, true, BaseArrayBean.class) {
                        @Override
                        public void doWork(BaseArrayBean data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            if (tag == 10 ) {
                                if (code.equals("1")) {
                                    ToastUtils.showShort("提交意向成功");
                                    startActivity(MyPurposeActivity.class);
                                    finish();

                                } else {
                                    try {
                                        ToastUtils.showShort(obj.getString("msg"));
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            } else {
                                bean.setUsername(etName.getText().toString() + (radioGroup.getCheckedRadioButtonId()==R.id.man?"先生":"女士"));
                                bean.setUserPhone(publishPhone);
                                startActivity(new Intent(SubmitMessageActivity.this,RetentionMoneyActivity.class)
                                        .putExtra("info_mation",bean));
                            }

                        }
                    }, true);
        } catch (Exception e) {
        }
    }

}
